/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010, 2010

    This file is part of Maxsi Engine.

    Maxsi Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiEngineContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiEngineContributors.h. For more information please see
	MaxsiEngineContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiEngine.dll
	A linkable version of Maxsi Engine

	MaxsiUI_Control_Label.cpp
	A label window.

****************************************************************************/

#include "MaxsiEngine.h"
#include "commctrl.h"

#ifndef __MINGW_H
#pragma warning( disable : 4312 )
#endif

namespace MaxsiEngine
{

	bool MaxsiUI_Control_Label_Initialized = false;

	LINK bool MaxsiUI_Control_Label_Initialize(HANDLE hInstance)
	{		 
		if ( MaxsiUI_Control_Label_Initialized ) { return true; }
		
		WNDCLASS  wc = {0};

		wc.lpfnWndProc = (WNDPROC) MaxsiUI_Control_WindowProcA;
		wc.hInstance = (HINSTANCE)hInstance;
		wc.hCursor = LoadCursor(NULL, IDC_ARROW);
		wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); 
		wc.lpszClassName = TEXT("MaxsiUI_Control_LabelA");

		RegisterClass(&wc);

		wc.lpfnWndProc = (WNDPROC) MaxsiUI_Control_WindowProcW;
		wc.hInstance = (HINSTANCE)hInstance;
		wc.hCursor = LoadCursor(NULL, IDC_ARROW);
		wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
		wc.lpszClassName = TEXT("MaxsiUI_Control_LabelW");

		RegisterClass(&wc);

		return true;
	}

	LRESULT APIENTRY MaxsiUI_Control_WindowProcW(
    HWND hWnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
	{
		MaxsiUI_ControlInstance* MaxsiUI_ControlInstance = GetControlInstance(hWnd);
		MaxsiUI_Control_Label_Instance* Instance = dynamic_cast<MaxsiUI_Control_Label_Instance*>(MaxsiUI_ControlInstance);
		if ( !Instance && message != WM_CREATE && message != WM_NCCREATE && message != WM_DESTROY)
		{
			return (0);
		}

		//HDC         hDC         = NULL;
		//PAINTSTRUCT ps          = {0};
		//RECT        rectClient  = {0};

		switch (message) 
		{
			case WM_CREATE:
				if (!GetControlInstance(hWnd))
				{
					HFONT hFont=CreateFont (13, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET,
						OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
						DEFAULT_PITCH | FF_SWISS, TEXT("Verdana"));

					MaxsiUI_Control_Label_Instance* Instance = new MaxsiUI_Control_Label_Instance;
					Instance->WindowHandle = hWnd;
					Instance->Font = hFont;
					Instance->Text = 0;
					AddControlInstance(Instance);
				}
				break;
			//case WM_COMMAND:
			//	switch(LOWORD(wParam)) 
			//	{
			//	case: 
			//	break;
			//	}
			case WM_SIZE:
				//if ( CurrentPage )
				//{
				//	CurrentPage->NewResolution(LOWORD(lParam),HIWORD(lParam));
				//}
				InvalidateRect(hWnd, NULL, TRUE);
				break;

			case WM_SETFONT:
				DeleteObject(Instance->Font);
				Instance->Font = (HFONT)wParam;
				break;
			case WM_SETTEXT:
				Instance->Text = BuildString(1,(char*)wParam);
				InvalidateRect(hWnd, NULL, TRUE);
				break;
			case WM_PAINT:
				if ( true )
				{
					PAINTSTRUCT ps          = {0};
					HDC hDC = BeginPaint (hWnd, &ps);
					
					if ( Instance->Text && Instance->Font)
					{
						RECT TextRect;

						GetClientRect (hWnd, &TextRect);
						SetBkMode(hDC,TRANSPARENT);
						SetTextColor(hDC,RGB(0,0,0));
		   
						SelectObject(hDC,Instance->Font);

						DrawTextA (hDC, Instance->Text, -1, &TextRect, DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
					}
					EndPaint (hWnd, &ps);
				}
				break;

			case WM_DESTROY:
				if ( Instance )
				{
					if ( Instance->Font ) { DeleteObject(Instance->Font); }
					if ( Instance->Text ) { delete[] Instance->Text; }
				}
				DeleteControlInstance(hWnd);
				break;

			default:
				return (DefWindowProc(hWnd, message, wParam, lParam));
				break;
		}
		return (0);
	}

	LRESULT APIENTRY MaxsiUI_Control_WindowProcA(
    HWND hWnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
	{
		MaxsiUI_ControlInstance* MaxsiUI_ControlInstance = GetControlInstance(hWnd);
		MaxsiUI_Control_Label_Instance* Instance = dynamic_cast<MaxsiUI_Control_Label_Instance*>(MaxsiUI_ControlInstance);
		if ( !Instance && message != WM_CREATE && message != WM_NCCREATE && message != WM_DESTROY)
		{
			return (0);
		}

		//HDC         hDC         = NULL;
		//PAINTSTRUCT ps          = {0};
		//RECT        rectClient  = {0};

		switch (message) 
		{
			case WM_CREATE:
				if (!GetControlInstance(hWnd))
				{
					HFONT hFont=CreateFont (13, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET,
						OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
						DEFAULT_PITCH | FF_SWISS, TEXT("Verdana"));

					MaxsiUI_Control_Label_Instance* Instance = new MaxsiUI_Control_Label_Instance;
					Instance->WindowHandle = hWnd;
					Instance->Font = hFont;
					Instance->Text = 0;
					AddControlInstance(Instance);
				}
				break;
			//case WM_COMMAND:
			//	switch(LOWORD(wParam)) 
			//	{
			//	case: 
			//	break;
			//	}
			case WM_SIZE:
				//if ( CurrentPage )
				//{
				//	CurrentPage->NewResolution(LOWORD(lParam),HIWORD(lParam));
				//}
				InvalidateRect(hWnd, NULL, TRUE);
				break;

			case WM_SETFONT:
				DeleteObject(Instance->Font);
				Instance->Font = (HFONT)wParam;
				break;
			case WM_SETTEXT:
				Instance->Text = BuildString(1,(char*)wParam);
				InvalidateRect(hWnd, NULL, TRUE);
				break;
			case WM_PAINT:
				if ( true )
				{
					PAINTSTRUCT ps          = {0};
					HDC hDC = BeginPaint (hWnd, &ps);
					
					if ( Instance->Text && Instance->Font)
					{
						RECT TextRect;

						GetClientRect (hWnd, &TextRect);
						SetBkMode(hDC,TRANSPARENT);
						SetTextColor(hDC,RGB(0,0,0));
		   
						SelectObject(hDC,Instance->Font);

						DrawTextA (hDC, Instance->Text, -1, &TextRect, DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
					}
					EndPaint (hWnd, &ps);
				}
				break;

			case WM_DESTROY:
				if ( Instance )
				{
					if ( Instance->Font ) { DeleteObject(Instance->Font); }
					if ( Instance->Text ) { delete[] Instance->Text; }
				}
				DeleteControlInstance(hWnd);
				break;

			default:
				return (DefWindowProc(hWnd, message, wParam, lParam));
				break;
		}
		return (0);
	}



}
